Γεια σας παιδιά,
Εχω φτιάξει ενα προγραμμα το οποίο διαβάζει φωτογραφίες απο ενα φακελο, αλλάζει το Resolution και τα κανει upload σε ενα ftp server.
το πρόβλημα ειναι ότι καποια στιγμή πετάει μηνυμα "System.OutOfMemoryException".
Τα αρχεία που θέλω να ανεβάσω ειναι περίπου 400-500 προς το παρόν.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using System.Net;
namespace myCustom
{
class uploadPic
{
string _ftpPath = "";
string _fromPicdir = "";
Int32 fileCount = 0;
Int32 i = 1;
public void loadFiles(string fromPicdir, string ftpPath)
{
_fromPicdir = fromPicdir;
_ftpPath = ftpPath;
string[] fileEntries = Directory.GetFiles(_fromPicdir);
fileCount = Directory.GetFiles(_fromPicdir).Length;
if (fileCount > 0)
{
foreach (string fileName in fileEntries)
{
chRes(fileName);
}
}
}
private void chRes(string _fileName)
{
using (Bitmap bitmap = (Bitmap)Image.FromFile(_fileName))
{
using (Bitmap newBitmap = new Bitmap(bitmap))
{
newBitmap.SetResolution(72, 72);
try
{
newBitmap.Save(Path.GetTempPath() + "\\" + Path.GetFileName(_fileName), ImageFormat.Jpeg);
uploadFile(_ftpPath
, "myUser"
, "myPass"
, Path.GetTempPath() + "/" + Path.GetFileName(_fileName)
);
}
catch (Exception ex)
{
string asd = ex.Message;
}
}
}
}
private void uploadFile(string ftpurl, string ftpusername, string ftppassword, string fileName)
{
try
{
string filename = Path.GetFileName(fileName);
string ftpfullpath = ftpurl + Path.GetFileName(fileName);
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(fileName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
catch (Exception ex)
{
MessageBox.Show("There was an error connecting to the FTP Server");
}
}
}
}